package com.tabuto.jizy.ejb3;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.tabuto.jizy.dao.ContactDao;
import com.tabuto.jizy.dao.UserDao;
import com.tabuto.jizy.dao.jdbc.ContactJDBCDao;
import com.tabuto.jizy.dao.jdbc.UserJDBCDao;
import com.tabuto.jizy.models.AbstractUser;
import com.tabuto.jizy.models.ContactModel;
import com.tabuto.jizy.models.UserModel;

/**
 * Session Bean implementation class JizyEjb
 */
@WebService(name = "Jizy", serviceName = "JizyService")
@Stateless(mappedName="JizyEjb")
@LocalBean
public class JizyEjb implements JizyEjbRemote, JizyEjbLocal {
	
	private ContactDao contactDao = null;
	
	private UserDao userDao = null;
	

    /**
     * Default constructor. 
     */
    public JizyEjb() {}

	@Override
	@WebMethod
	public boolean registrerUser(@WebParam(name = "username")String nickname,@WebParam(name = "password")String password) {
		AbstractUser user = new UserModel();
		user.setUsername(nickname);
		user.setPassword(password);
		return getUserDao().saveUser(user);
		
	}

	@Override
	@WebMethod
	public boolean addContact(@WebParam(name = "contact")ContactModel contact,@WebParam(name = "account") AbstractUser user) {
		return getContactDao().addContact(contact, user);
	}

	@Override
	public List<ContactModel> findContacts(ContactModel filter,
			AbstractUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@WebMethod
	public List<ContactModel> showContacts(@WebParam(name = "account")AbstractUser user) {
		return getContactDao().getAllContact(user);
	}

	private ContactDao getContactDao() {
		if(contactDao==null)
			contactDao = new ContactJDBCDao();
	
		return contactDao;		
	}

	private UserDao getUserDao() {
		if(userDao == null)
			userDao = new UserJDBCDao();
		
		return userDao;
	}

	
	
	
	

}
